package com.ssy.lingxi.report.serviceimpl.tally;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.report.api.enums.ContractApplyAmountOrderOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.ContractCoordinationOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.ContractManageOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.report.dao.contract.*;
import com.ssy.lingxi.report.entity.report.MemberTallyReportDO;
import com.ssy.lingxi.report.model.bo.MemberTallyReportBO;
import com.ssy.lingxi.report.model.bo.tally.ContractApplyAmountOrderTallyBO;
import com.ssy.lingxi.report.model.bo.tally.ContractCoordinationTallyBO;
import com.ssy.lingxi.report.model.bo.tally.ContractManageTallyBO;
import com.ssy.lingxi.report.model.bo.tally.statistics.ContractApplyAmountOrderStatisticsTallyVO;
import com.ssy.lingxi.report.model.bo.tally.statistics.ContractCoordinationStatisticsTallyVO;
import com.ssy.lingxi.report.model.bo.tally.statistics.ContractManageStatisticsTallyVO;
import com.ssy.lingxi.report.model.vo.response.ContractTallyReportVO;
import com.ssy.lingxi.report.model.vo.response.TallyReportItemVO;
import com.ssy.lingxi.report.service.tally.IContractTallyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  合同计数接口实现
 * @author ds
 * @date 2021/5/11
 * @version 2.0.0
 */
@Service
public class ContractTallyServiceImpl implements IContractTallyService {

    private static final Logger logger = LoggerFactory.getLogger(ContractTallyServiceImpl.class);

    @Resource
    private ContractDAO contractDAO;
    @Resource
    private ConPurchaseInquiryDAO conPurchaseInquiryDAO;
    @Resource
    private ConPurchaseInviteBidDAO conPurchaseInviteBidDAO;
    @Resource
    private ConPurchaseViePriceDAO conPurchaseViePriceDAO;
    @Resource
    private ApplyAmountOrderDAO applyAmountOrderDAO;
    /**
     * 转换合同
     * @param reportBO 计数报表
     * @return 售后计数报表
     */
    @Override
    public ContractTallyReportVO convertContractTally(MemberTallyReportBO reportBO) {
        ContractTallyReportVO result = new ContractTallyReportVO();
        // 转换合同管理数据
        result.setContractManageList(convertContractManageTally(reportBO.getContractMange()));
        // 转换合同协同数据
        result.setContractCoordinationList(convertContractCoordinationTally(reportBO.getContractCoordination()));
        // 转换请款单数据
        result.setApplyAmountOrderList(convertApplyAmountOrderTally(reportBO.getContractApplyAmountOrder()));
        return result;
    }

    /**
     * 转换售后维修申请数据
     * @param tallyBO 计数报表
     * @return 计数报表项
     */
    private List<TallyReportItemVO> convertContractManageTally(ContractManageTallyBO tallyBO) {

        if (tallyBO == null) {
            tallyBO = new ContractManageTallyBO();
        }

        List<TallyReportItemVO> resultList = new ArrayList<>();
        // 待创建采购询价合同
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCreateInquiryCount() > 0 ? tallyBO.getToBeCreateInquiryCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_CREATE_INQUIRY.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_CREATE_INQUIRY.getLink());
        resultList.add(tallyReportItem);
        // 待创建采购竞价合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCreateViePriceCount() > 0 ? tallyBO.getToBeCreateViePriceCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_CREATE_VIE_PRICE.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_CREATE_VIE_PRICE.getLink());
        resultList.add(tallyReportItem);
        // 待创建采购招标合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCreateInviteTenderCount() > 0 ? tallyBO.getToBeCreateInviteTenderCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_CREATE_INVITE_TENDER.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_CREATE_INVITE_TENDER.getLink());
        resultList.add(tallyReportItem);
        // 待提交审核合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitCount() > 0 ? tallyBO.getToBeCommitCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_COMMIT.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_COMMIT.getLink());
        resultList.add(tallyReportItem);
        // 待提交审核合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitValifyCount() > 0 ? tallyBO.getToBeCommitValifyCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_COMMIT_VALIFY.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_COMMIT_VALIFY.getLink());
        resultList.add(tallyReportItem);
        // 待审核合同（一级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep1Count() > 0 ? tallyBO.getToBeValifyStep1Count() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_VALIFY_STEP1.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_VALIFY_STEP1.getLink());
        resultList.add(tallyReportItem);
        // 待审核合同（二级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep2Count() > 0 ? tallyBO.getToBeValifyStep2Count() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_VALIFY_STEP2.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_VALIFY_STEP2.getLink());
        resultList.add(tallyReportItem);
        // 待签订合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeSignCount() > 0 ? tallyBO.getToBeSignCount() : 0);
        tallyReportItem.setName(ContractManageOperateTypeEnum.TO_BE_SIGN.getName());
        tallyReportItem.setLink(ContractManageOperateTypeEnum.TO_BE_SIGN.getLink());
        resultList.add(tallyReportItem);
        return resultList;
    }

    /**
     * 转换合同协同数据
     * @param tallyBO 计数报表
     * @return 计数报表项
     */
    private List<TallyReportItemVO> convertContractCoordinationTally(ContractCoordinationTallyBO tallyBO) {
        if (tallyBO == null) {
            tallyBO = new ContractCoordinationTallyBO();
        }
        List<TallyReportItemVO> resultList = new ArrayList<>();
        // 待提交审核合同
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitValifyCount() > 0 ? tallyBO.getToBeCommitValifyCount() : 0);
        tallyReportItem.setName(ContractCoordinationOperateTypeEnum.TO_BE_COMMIT_VALIFY.getName());
        tallyReportItem.setLink(ContractCoordinationOperateTypeEnum.TO_BE_COMMIT_VALIFY.getLink());
        resultList.add(tallyReportItem);
        // 待审核合同（一级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep1Count() > 0 ? tallyBO.getToBeValifyStep1Count() : 0);
        tallyReportItem.setName(ContractCoordinationOperateTypeEnum.TO_BE_VALIFY_STEP1.getName());
        tallyReportItem.setLink(ContractCoordinationOperateTypeEnum.TO_BE_VALIFY_STEP1.getLink());
        resultList.add(tallyReportItem);
        // 待审核合同（二级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep2Count() > 0 ? tallyBO.getToBeValifyStep2Count() : 0);
        tallyReportItem.setName(ContractCoordinationOperateTypeEnum.TO_BE_VALIFY_STEP2.getName());
        tallyReportItem.setLink(ContractCoordinationOperateTypeEnum.TO_BE_VALIFY_STEP2.getLink());
        resultList.add(tallyReportItem);
        // 待签订合同
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeSignCount() > 0 ? tallyBO.getToBeSignCount() : 0);
        tallyReportItem.setName(ContractCoordinationOperateTypeEnum.TO_BE_SIGN.getName());
        tallyReportItem.setLink(ContractCoordinationOperateTypeEnum.TO_BE_SIGN.getLink());
        resultList.add(tallyReportItem);

        return resultList;
    }

    /**
     * 转换合同请款数据
     * @param tallyBO 计数报表
     * @return 计数报表项
     */
    private List<TallyReportItemVO> convertApplyAmountOrderTally(ContractApplyAmountOrderTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new ContractApplyAmountOrderTallyBO();
        }
        // 待提交审核请款单
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitValifyCount() > 0 ? tallyBO.getToBeCommitValifyCount() : 0);
        tallyReportItem.setName(ContractApplyAmountOrderOperateTypeEnum.TO_BE_COMMIT_VALIFY.getName());
        tallyReportItem.setLink(ContractApplyAmountOrderOperateTypeEnum.TO_BE_COMMIT_VALIFY.getLink());
        resultList.add(tallyReportItem);
        // 待审核请款单（一级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep1Count() > 0 ? tallyBO.getToBeValifyStep1Count() : 0);
        tallyReportItem.setName(ContractApplyAmountOrderOperateTypeEnum.TO_BE_VALIFY_STEP1.getName());
        tallyReportItem.setLink(ContractApplyAmountOrderOperateTypeEnum.TO_BE_VALIFY_STEP1.getLink());
        resultList.add(tallyReportItem);
        // 待审核请款单（二级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep2Count() > 0 ? tallyBO.getToBeValifyStep2Count() : 0);
        tallyReportItem.setName(ContractApplyAmountOrderOperateTypeEnum.TO_BE_VALIFY_STEP2.getName());
        tallyReportItem.setLink(ContractApplyAmountOrderOperateTypeEnum.TO_BE_VALIFY_STEP2.getLink());
        resultList.add(tallyReportItem);
        // 待提交请款单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitCount() > 0 ? tallyBO.getToBeCommitCount() : 0);
        tallyReportItem.setName(ContractApplyAmountOrderOperateTypeEnum.TO_BE_COMMIT.getName());
        tallyReportItem.setLink(ContractApplyAmountOrderOperateTypeEnum.TO_BE_COMMIT.getLink());
        resultList.add(tallyReportItem);
        return resultList;
    }



    /**
     * 设置合同
     * @param tallyReportBO 会员计数报表
     * @param noticeVO 操作通知VO
     * @return 会员计数报表
     */
    @Override
    public MemberTallyReportBO setContractTally(MemberTallyReportBO tallyReportBO, OperateNoticeVO noticeVO) {

        OperateDataSourceEnum sourceEnum = OperateDataSourceEnum.getEnum(noticeVO.getDataSource());
        switch (sourceEnum) {
            case CONTRACT_MANAGE: // 转换合同管理数据
                tallyReportBO.setContractMange(setContractMangeTally(tallyReportBO.getContractMange(), noticeVO));
                break;
            case CONTRACT_COORDINATION: // 转换合同协同数据
                tallyReportBO.setContractCoordination(setContractCoordinationTally(tallyReportBO.getContractCoordination(), noticeVO));
                break;
            case CONTRACT_APPLY_AMOUNT_ORDER: // 转换合同请款数据
                tallyReportBO.setContractApplyAmountOrder(setContractApplyAmountOrderTally(tallyReportBO.getContractApplyAmountOrder(), noticeVO));
                break;
        }
        return tallyReportBO;
    }

    /**
     * 设置售后退货处理数据
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private ContractManageTallyBO setContractMangeTally(ContractManageTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new ContractManageTallyBO();
        }

        // 更新数据
        ContractManageOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = ContractManageOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_CREATE_INQUIRY:
                        tallyBO.setToBeCreateInquiryCount(tallyBO.getToBeCreateInquiryCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_CREATE_VIE_PRICE:
                        tallyBO.setToBeCreateViePriceCount(tallyBO.getToBeCreateViePriceCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_CREATE_INVITE_TENDER:
                        tallyBO.setToBeCreateInviteTenderCount(tallyBO.getToBeCreateInviteTenderCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_SIGN:
                        tallyBO.setToBeSignCount(tallyBO.getToBeSignCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = ContractManageOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_CREATE_INQUIRY:
                        tallyBO.setToBeCreateInquiryCount(tallyBO.getToBeCreateInquiryCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_CREATE_VIE_PRICE:
                        tallyBO.setToBeCreateViePriceCount(tallyBO.getToBeCreateViePriceCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_CREATE_INVITE_TENDER:
                        tallyBO.setToBeCreateInviteTenderCount(tallyBO.getToBeCreateInviteTenderCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_SIGN:
                        tallyBO.setToBeSignCount(tallyBO.getToBeSignCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置合同协同数据
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private ContractCoordinationTallyBO setContractCoordinationTally(ContractCoordinationTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new ContractCoordinationTallyBO();
        }
        // 更新数据
        ContractCoordinationOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = ContractCoordinationOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_SIGN:
                        tallyBO.setToBeSignCount(tallyBO.getToBeSignCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = ContractCoordinationOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_SIGN:
                        tallyBO.setToBeSignCount(tallyBO.getToBeSignCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置合同请款数据
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private ContractApplyAmountOrderTallyBO setContractApplyAmountOrderTally(ContractApplyAmountOrderTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new ContractApplyAmountOrderTallyBO();
        }
        // 更新数据
        ContractApplyAmountOrderOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = ContractApplyAmountOrderOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = ContractApplyAmountOrderOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_COMMIT_VALIFY:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }


    /**
     * 统计计数-合同
     * @param reportList
     * @return
     */
    @Override
    public List<MemberTallyReportDO> statisticsTally(List<MemberTallyReportDO> reportList) {

        // 合同管理
        reportList = statisticsContractMangeTally(reportList);
        // 合同协同
        reportList = statisticsContractCoordinationTally(reportList);
        // 合同请款
        reportList = statisticsApplyAmountOrderTally(reportList);
        return reportList;
    }

    /**
     * 统计合同管理
     * @param reportList 会员计数报表
     * @return 会员计数报表
     */
    private List<MemberTallyReportDO> statisticsContractMangeTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        Map<String, MemberTallyReportDO> dbReportList = new HashMap<>();
        for (MemberTallyReportDO tallyReportDO : reportList) {
            dbReportList.put(tallyReportDO.getMemberId() + ":" + tallyReportDO.getRoleId(), tallyReportDO);
        }

        List<ContractManageStatisticsTallyVO> statusStatisticsCount = contractDAO.getStatusStatisticsCount();
        if(CollectionUtil.isNotEmpty(statusStatisticsCount)){
            statusStatisticsCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    ContractManageTallyBO bo=new ContractManageTallyBO();
                    BeanUtils.copyProperties(o,bo);
                    bo.setToBeCreateInviteTenderCount(0L);
                    bo.setToBeCreateInquiryCount(0L);
                    bo.setToBeCreateViePriceCount(0L);
                    memberTallyReportDO.getTallyReport().setContractMange(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractManageTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractMange();
                    if(contractMange!=null){
                        BeanUtils.copyProperties(o,contractMange);
                        contractMange.setToBeCreateInviteTenderCount(0L);
                        contractMange.setToBeCreateInquiryCount(0L);
                        contractMange.setToBeCreateViePriceCount(0L);
                    }else{
                        contractMange=new ContractManageTallyBO();
                        BeanUtils.copyProperties(o,contractMange);
                    }
                    memberTallyReportDO.getTallyReport().setContractMange(contractMange);
                }
            });
        }
        //待创建询价合同
        List<ContractManageStatisticsTallyVO> inquiryCount = conPurchaseInquiryDAO.getStatisticsCount();
        if(CollectionUtil.isNotEmpty(inquiryCount)){
            inquiryCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    memberTallyReportDO.setMemberId(o.getMemberId());
                    memberTallyReportDO.setRoleId(o.getRoleId());
                    MemberTallyReportBO memberTallyReportBO=new MemberTallyReportBO();
                    memberTallyReportDO.setTallyReport(memberTallyReportBO);
                    ContractManageTallyBO bo=new ContractManageTallyBO();
                    bo.setToBeCreateInquiryCount(o.getToBeCreateInquiryCount()==null?0L:o.getToBeCreateInquiryCount());
                    memberTallyReportDO.getTallyReport().setContractMange(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractManageTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractMange();
                    if(contractMange==null){
                        contractMange=new ContractManageTallyBO();
                    }
                    contractMange.setToBeCreateInquiryCount(o.getToBeCreateInquiryCount()==null?0L:o.getToBeCreateInquiryCount());
                    memberTallyReportDO.getTallyReport().setContractMange(contractMange);
                }
            });
        }
        //待创建招标合同
        List<ContractManageStatisticsTallyVO> inviteCount = conPurchaseInviteBidDAO.getStatisticsCount();
        if(CollectionUtil.isNotEmpty(inviteCount)){
            inviteCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    memberTallyReportDO.setMemberId(o.getMemberId());
                    memberTallyReportDO.setRoleId(o.getRoleId());
                    MemberTallyReportBO memberTallyReportBO=new MemberTallyReportBO();
                    memberTallyReportDO.setTallyReport(memberTallyReportBO);
                    ContractManageTallyBO bo=new ContractManageTallyBO();
                    bo.setToBeCreateInviteTenderCount(o.getToBeCreateInviteTenderCount()==null?0L:o.getToBeCreateInviteTenderCount());
                    memberTallyReportDO.getTallyReport().setContractMange(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractManageTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractMange();
                    if(contractMange==null){
                        contractMange=new ContractManageTallyBO();
                    }
                    contractMange.setToBeCreateInviteTenderCount(o.getToBeCreateInviteTenderCount()==null?0L:o.getToBeCreateInviteTenderCount());
                    memberTallyReportDO.getTallyReport().setContractMange(contractMange);
                }
            });
        }
        //待创建竞价合同
        List<ContractManageStatisticsTallyVO> viePriceCount = conPurchaseViePriceDAO.getStatisticsCount();
        if(CollectionUtil.isNotEmpty(viePriceCount)){
            viePriceCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    memberTallyReportDO.setMemberId(o.getMemberId());
                    memberTallyReportDO.setRoleId(o.getRoleId());
                    MemberTallyReportBO memberTallyReportBO=new MemberTallyReportBO();
                    memberTallyReportDO.setTallyReport(memberTallyReportBO);
                    ContractManageTallyBO bo=new ContractManageTallyBO();
                    bo.setToBeCreateViePriceCount(o.getToBeCreateViePriceCount()==null?0L:o.getToBeCreateViePriceCount());
                    memberTallyReportDO.getTallyReport().setContractMange(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractManageTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractMange();
                    if(contractMange==null){
                        contractMange=new ContractManageTallyBO();
                    }
                    contractMange.setToBeCreateViePriceCount(o.getToBeCreateViePriceCount()==null?0L:o.getToBeCreateViePriceCount());
                    memberTallyReportDO.getTallyReport().setContractMange(contractMange);
                }
            });
        }
        return new ArrayList<>(dbReportList.values());
    }

    /**
     * 统计合同协同
     * @param reportList 会员计数报表
     * @return 会员计数报表
     */
    private List<MemberTallyReportDO> statisticsContractCoordinationTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        Map<String, MemberTallyReportDO> dbReportList = reportList.stream().collect(Collectors.toMap(o -> o.getMemberId() + ":" + o.getRoleId(), o -> o,(value1, value2 )-> value2));

        List<ContractCoordinationStatisticsTallyVO> statusStatisticsCount = contractDAO.getStatusStatisticsCountByPartyB();
        if(CollectionUtil.isNotEmpty(statusStatisticsCount)){
            statusStatisticsCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    memberTallyReportDO.setMemberId(o.getMemberId());
                    memberTallyReportDO.setRoleId(o.getRoleId());
                    MemberTallyReportBO memberTallyReportBO=new MemberTallyReportBO();
                    memberTallyReportDO.setTallyReport(memberTallyReportBO);
                    ContractCoordinationTallyBO bo=new ContractCoordinationTallyBO();
                    BeanUtils.copyProperties(o,bo);
                    memberTallyReportDO.getTallyReport().setContractCoordination(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractCoordinationTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractCoordination();
                    if(contractMange==null){
                        contractMange=new ContractCoordinationTallyBO();
                    }
                    BeanUtils.copyProperties(o,contractMange);
                    memberTallyReportDO.getTallyReport().setContractCoordination(contractMange);
                }
            });
        }
        return new ArrayList<>(dbReportList.values());
    }

    /**
     * 统计请款
     * @param reportList 会员计数报表
     * @return 会员计数报表
     */
    private List<MemberTallyReportDO> statisticsApplyAmountOrderTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        Map<String, MemberTallyReportDO> dbReportList = reportList.stream().collect(Collectors.toMap(o -> o.getMemberId() + ":" + o.getRoleId(), o -> o,(value1, value2 )-> value2));

        List<ContractApplyAmountOrderStatisticsTallyVO> statusStatisticsCount = applyAmountOrderDAO.getStatusStatisticsCount();
        if(CollectionUtil.isNotEmpty(statusStatisticsCount)){
            statusStatisticsCount.forEach(o ->{
                String key=o.getMemberId()+":"+o.getRoleId();
                MemberTallyReportDO memberTallyReportDO = dbReportList.get(key);
                if(memberTallyReportDO==null){
                    memberTallyReportDO=new MemberTallyReportDO();
                    memberTallyReportDO.setMemberId(o.getMemberId());
                    memberTallyReportDO.setRoleId(o.getRoleId());
                    MemberTallyReportBO memberTallyReportBO=new MemberTallyReportBO();
                    memberTallyReportDO.setTallyReport(memberTallyReportBO);
                    ContractApplyAmountOrderTallyBO bo=new ContractApplyAmountOrderTallyBO();
                    BeanUtils.copyProperties(o,bo);
                    memberTallyReportDO.getTallyReport().setContractApplyAmountOrder(bo);
                    dbReportList.put(key,memberTallyReportDO);
                }else{
                    ContractApplyAmountOrderTallyBO contractMange = memberTallyReportDO.getTallyReport().getContractApplyAmountOrder();
                    if(contractMange==null){
                        contractMange=new ContractApplyAmountOrderTallyBO();
                    }
                    BeanUtils.copyProperties(o,contractMange);
                    memberTallyReportDO.getTallyReport().setContractApplyAmountOrder(contractMange);
                }
            });
        }
        return new ArrayList<>(dbReportList.values());
    }


}
